package com.wechat.Util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import net.sf.json.JSONObject;

import com.wechat.po.AccessToken;
/**
 * 通用工具类
 * 
 * @author chenpeng
 * @date 2015-02-25
 */
public class CommonUtil {
	private static final String APPID ="wxdc1dbcc5fd398fc4";
	private static final String APPSECRET = "65820cb487c99064f457324cd9a25ea5";
	//获取Access_Token的请求地址
	private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	/**
	 * 处理https GET/POST请求
	 * @param requestUrl 请求地址
	 * @param requestMethod 请求方法
	 * @param outputStr 参数
	 * @return
	 */
	public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){
		StringBuffer buffer = null;
		try{
		//创建SSL Context
		SSLContext sslContext = SSLContext.getInstance("SSL","SunJSSE");
		TrustManager[] tm = {new MyX509TrustManager()};
		//初始化
		sslContext.init(null, tm, new java.security.SecureRandom());
		//获取SSL SocketFactory对象
		SSLSocketFactory ssf = sslContext.getSocketFactory();
		
		
		URL url = new URL(requestUrl);
		HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
		connection.setRequestMethod(requestMethod);
		//设置当前实例使用的SSL SocketFactory
		connection.setSSLSocketFactory(ssf);
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.connect();
		
		//往服务器端写内容
		if(null != outputStr){
			OutputStream os = connection.getOutputStream();
			os.write(outputStr.getBytes("utf-8"));
		}
		
		//读取服务器端返回内容
		InputStream is = connection.getInputStream();
		InputStreamReader isr =new InputStreamReader(is, "utf-8");
		BufferedReader br = new BufferedReader(isr);
		buffer = new StringBuffer();
		String line = null;
		while((line = br.readLine())!=null){
			buffer.append(line);
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return buffer.toString();
		}
	
	/**
	 * 获取access-token
	 * @param appid 公众号的唯一凭证
	 * @param appsecret 密钥
	 * @return
	 */
	public static AccessToken getAccessToken(){
		AccessToken token = new AccessToken();
		//拼接请求地址
		String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
		//调用接口
		String jsonStr = CommonUtil.httpsRequest(url, "GET", null);
		try{
		//将Json字符串转换为Java对象
		JSONObject jsonObject = JSONObject.fromObject(jsonStr);
		
		String AccessToken = jsonObject.getString("access_token");
		int expiresIn = jsonObject.getInt("expires_in");
			token.setToken(AccessToken);
			token.setExpires(expiresIn);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return token;
	}
	/**
	 * URL编码
	 * @param source
	 * @return
	 */
	public static String urlEncodingUTF8(String source){
		String result = source;
		try{
			result = java.net.URLEncoder.encode(source,"UTF-8");
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

